#!/usr/bin/env python
# @Software: PyCharm
# @Time : 2022/9/20 19:45
# @Author : v_nahuachen
# @Version：V 0.1
# @File : login_test.py
# @desc :
from cases.base import router
from cases.base.base_case import BaseCase
from cases.pages import home_page
from cases.pages import base_page


class LoginTest(BaseCase):

    @classmethod
    def setUpClass(cls) -> None:
        super().setUpClass()

    def setUp(self) -> None:
        super().setUp()
        # 页面跳转
        self.HomePage.switch_to_tabbar("/pages/mine/mine")
        # 清空授权
        self.mini.clear_auth()

    def __init__(self, methodName='runTest'):
        """
        初始化登录类
        """
        super(LoginTest, self).__init__(methodName)
        self.HomePage = home_page.HomePage(self)
        self.BasePage = base_page.BasePage(self)

    def test_wechat_login(self):
        """
        微信登录
        """
        self.BasePage.get_element_xpath("/page/view/navigator[1]/button").tap()
        self.page.wait_for(2)
        ret = self.BasePage.wait_page(router.loginPage)
        self.assertTrue(ret, "页面跳转")

        # hook 页面方法,并获取回调信息
        is_called, callback_args = self.BasePage.hook_native_method(
            method="getUserProfile",
            selector="/page/view/button",
            attr_method="allow_get_user_info")

        self.assertTrue(is_called, "callback called")
        self.assertDictContainsSubset(
            {"errMsg": "getUserProfile:ok"}, callback_args, "授权用户信息")
    #
    def test_custom_login(self):
        """
        自定义账号登录，账号注册
        """
        self.BasePage.get_element_xpath("/page/view/navigator[2]/button").tap()
        self.page.wait_for(2)
        ret = self.BasePage.wait_page(router.selfLoginPage)
        self.assertTrue(ret, "页面跳转")

        els = self.BasePage.get_elements_selector(".weui-input")
        els[0].input("test1")
        els[1].input("123")

        # hook 页面方法,并获取回调信息
        is_called, callback_args = self.BasePage.hook_native_page_method(
            method="submitRegister",
            selector="/page/view/view[3]/button",
            attr_method="allow_get_user_info")
        # 页面方法是否有回调
        self.assertTrue(is_called, "callback called")

    def test_custom_exist_login(self):
        """
        自定义账号登录，绑定已有帐号
        """
        self.BasePage.get_element_xpath("/page/view/navigator[2]/button").tap()
        self.page.wait_for(2)
        ret = self.BasePage.wait_page(router.selfLoginPage)
        self.assertTrue(ret, "页面跳转")

        els = self.BasePage.get_elements_selector(".weui-input")
        els[0].input("test8")
        els[1].input("123")

        self.BasePage.get_element_xpath("/page/view/radio-group/label[3]/radio").tap()

        # hook 页面方法,并获取回调信息
        is_called, callback_args = self.BasePage.hook_native_page_method(
            method="submitRegister",
            selector="/page/view/view[3]/button",
            attr_method="allow_get_user_info")

        self.assertTrue(is_called, "callback called")